<?php 
namespace app\api\controller\haipi;

use app\common\controller\Frontend;
use app\common\model\Area;
use app\common\model\haipi\OpenArea;
use think\facade\Db;

class Index extends  Frontend{

    protected array $noNeedLogin = ['*'];

    public function areas(){
        $rows = OpenArea::where("status",1)->append(['city'])->order('weigh', 'desc')->select();
        $this->success('', $rows);
    }

    public function subareas(){
        $pcode = $this->request->post('pcode');
        if(empty($pcode)){
            $this->error("缺少参数");
        }
        $pid = Area::where("code", $pcode)->value("id");
        $rows = Area::where('pid',$pid)->select();
        $this->success('', $rows);  
    }

    public function defaultLocation(){
        //默认地址
        $districtCode = $this->request->post('district_code');
        if(empty($districtCode)){
            $this->error("缺少参数");
        }
        
        $cityCode = Area::where('id', 'in',function($query)use($districtCode){
            $query->table('area')->where("code", $districtCode)->field('pid');
        })->value('code');
       
        $data = OpenArea::where('city_code', $cityCode)->append(['city'])->find();
        $this->success('', $data);
    }


    public function configs(){
        $this->success('',data: [
            'store_urls' => get_sys_config('store_urls', 'ticket_store')
        ]);
    }
}